
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for confirm_add_friend.
	''' </summary>
	Public Partial Class confirm_add_friend
		Inherits BaseWebPage
		Protected member As User = Nothing
		Protected htPageConstants As New Hashtable()


		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Auto login from an external link
			'AutoLogin( Request );

			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.ADD_FRIEND_CONFIRMATION;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_FRIEND_CONFIRMATION_PAGE, "", frmAddFriend, Nothing, htPageConstants)
			Me.initAdvertisements(frmAddFriend, Nothing)

			btnCancel.Attributes("onclick") = "javascript: history.back(); return false;"
			trValidateFriend.Visible = False

			Try
				If Request.Params("id") = Nothing OrElse Request.Params("id") = "" Then
					Response.Redirect(Constants.ERROR_PAGE + "?e=" + Constants.ERROR_MISSING_PARAMETERS)
				End If

				Dim id As Integer = Convert.ToInt32(Request.Params("id"))
				member = Users.GetUserByID(id)

				If member <> Nothing AndAlso member.ID <> usr.ID Then
					ucMemberShortcut.UserID = id
				Else
					trUserShortCut.Visible = False
				End If

				If member.ID = usr.ID Then
					lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgCannotAddYou_TEXT")
					lblFriendMessage.CssClass = "main_text_opposite_bold"
					lblFriendMessage.Visible = True
					btnAddToFriends.Visible = False
					btnCancel.Visible = False
					btnAccept.Visible = False
					btnReject.Visible = False
					Return
				End If

				If Request.Params(Constants.LOGIN_PARAM_USER) <> Nothing AndAlso Request.Params(Constants.LOGIN_PARAM_USER) <> "" Then
					lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQAddUser_TEXT")).Replace("[PARAM_1]", "<a href='" + Constants.MEMBERS + "?id=" + member.ID + "'>" + member.Name + "</a>")
					lblFriendMessage.Text = lblFriendMessage.Text.Replace("[PARAM_2]", member.Name)
					lblFriendMessage.Text = lblFriendMessage.Text.Replace("[PARAM_2]", member.Name)

					btnAccept.Visible = True
					btnReject.Visible = True
					btnAddToFriends.Visible = False
					btnCancel.Visible = False
				Else
					lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQSure_TEXT")).Replace("[PARAM]", member.Name)

					If Not member.ApproveFriends Then
						lblFriendMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgEnterNameEmail_TEXT")).Replace("[PARAM]", member.Name)
					End If

					trValidateFriend.Visible = True
					btnAccept.Visible = False
					btnReject.Visible = False
					btnAddToFriends.Visible = True
					btnCancel.Visible = True

					If Not member.ApproveFriends Then
						trValidateFriend.Visible = True
						Dim sValidateAtLeastOneFieldFilledScript As String = "<script>" + Environment.NewLine + "function validateRequiredFields(){" + Environment.NewLine + "" & Chr(9) & "var sLastName = document.forms[0]['" + txtLastName.UniqueID + "'].value;" + Environment.NewLine + "" & Chr(9) & "var sEmail = document.forms[0]['" + txtEmail.UniqueID + "'].value;" + Environment.NewLine + " var objErrorMessage = document.getElementById('_ctl0_lblEnterValue');" + Environment.NewLine + Environment.NewLine + " if ( objErrorMessage != null && objErrorMessage != 'undefined' && objErrorMessage != 'null' ){" + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "if ( sLastName == """" && sEmail == """" ){" + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "document.getElementById('_ctl0_lblEnterValue').style.display = 'block'; " + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "document.getElementById('_ctl0_lblEnterValue').style.visibility = 'visible'; " + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "return false;" + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "}else{" + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "document.getElementById('_ctl0_lblEnterValue').style.display = 'none'; " + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "document.getElementById('_ctl0_lblEnterValue').style.visibility = 'hidden'; " + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "" & Chr(9) & "return true;" + Environment.NewLine + "" & Chr(9) & "" & Chr(9) & "}" + Environment.NewLine + "" & Chr(9) & "}" + Environment.NewLine + "}" + Environment.NewLine + "</script>"

						Me.RegisterClientScriptBlock("client_val", sValidateAtLeastOneFieldFilledScript)
						Dim sOnClickAddScript As String = "javascript:validateRequiredFields();"
						frmAddFriend.Attributes.Add("onsubmit", sOnClickAddScript)
					Else
						trValidateFriend.Visible = False
					End If
				End If
			Catch
				lblFriendMessage.Text = Constants.ERROR_MESSAGE_MISSING_PARAMETERS
				lblFriendMessage.CssClass = "main_text_opposite_bold"
				btnAddToFriends.Visible = False
				btnCancel.Visible = False
			End Try
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnAddToFriends_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			lblMainTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_AddTitle_TEXT")

			If Request.Params("id") = "" Then
				Response.Redirect(Constants.ERROR_PAGE + "e=" + Constants.[ERROR])
			End If

			Try
				If (btnAccept.Visible) OrElse (member.ApproveFriends AndAlso btnAddToFriends.Visible) OrElse (Not member.ApproveFriends AndAlso isAtLeastOneFieldFilled() AndAlso btnAddToFriends.Visible) Then
					Dim id As Integer = Convert.ToInt32(Request.Params("id"))
					Dim user As User = Me.LoggedUser
					If user.DirectFriends.Contains(member.ID) Then
						lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgUserExist_TEXT")).Replace("[PARAM]", member.Name)
						lblFriendMessage.Visible = True
						lblFriendMessage.CssClass = "main_text_opposite_bold"
						btnAccept.Visible = False
						btnReject.Visible = False
					Else
						If member.ID = user.ID Then
							lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgCannotAdd_TEXT")
							lblFriendMessage.CssClass = "main_text_opposite_bold"
						Else
							Dim outgoingInv As UserInvitations = user.OutgoingInvitations
							Dim pendingInv As UserInvitations = user.PendingInvitations
							If Not outgoingInv.Contains(member.ID) Then
								If Not pendingInv.Contains(member.ID) Then
									user.AcceptCreateFriendship(member.ID)
									lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgEmailSent_TEXT")
								Else
									user.AcceptCreateFriendship(member.ID)

									If Request.Params(Constants.LOGIN_PARAM_USER) <> Nothing AndAlso Request.Params(Constants.LOGIN_PARAM_USER) <> "" Then
										lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgUserAdded_TEXT")).Replace("[PARAM]", member.Name)
									Else
										lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgUserInvited_TEXT")).Replace("[PARAM]", member.Name)
									End If

									btnAccept.Visible = False
									btnReject.Visible = False
								End If
							Else
								Dim pendingFriend As DirectFriend = Users.GetFriendInvitation(user.ID, member.ID)
								If pendingFriend.Status = DirectFriend.InviteStatus.dsRejected Then
									If pendingFriend.Attempts < Constants.FRIENDSHIP_MAX_REQUESTS Then
										' friendship request has been rejected but the user can send another request 
										user.AcceptCreateFriendship(member.ID)
										lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgEmailSent2_TEXT")
									Else
										lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgUserRejected_TEXT")).Replace("[PARAM]", member.Name)
										lblFriendMessage.Text += Me.GetPageConstText(htPageConstants, "CONST_MsgNoAttemptsLeft_TEXT")
									End If
ElseIf pendingFriend.Status = DirectFriend.InviteStatus.dsInitial Then
									lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgRequestSent_TEXT")).Replace("[PARAM]", member.Name)
								End If
							End If
						End If
					End If

					trValidateFriend.Visible = False
					btnCancel.Visible = False
					btnAddToFriends.Visible = False
				Else
					lblEnterValue.Visible = True
				End If
			Catch ex As Exception
				'lblFriendMessage.Text = "An unexpected error occured.Please try again.";
				lblFriendMessage.Text = ex.Message
				btnAddToFriends.Visible = False
				btnCancel.Visible = False
			End Try
		End Sub

		'		private void btnAccept_Click(object sender, System.EventArgs e)
'		{
'			lblMainTitle.Text = "Add To Friends";
'			if(Request.Params["id"] == "")
'				Response.Redirect(Constants.ERROR_PAGE + "e=" + Constants.ERROR);
'
'			try
'			{
'				int id = Convert.ToInt32(Request.Params["id"]);	
'				User member = Users.GetUserByID(id);
'				User user = this.LoggedUser;
'				if (user.DirectFriends.Contains(member.ID))
'				{
'					lblFriendMessage.Text ="<b>" + member.Name + "</b> already exists in your direct friends list.";
'					lblFriendMessage.Visible = true;
'
'				}
'				else
'				{	
'					if(member.ID == user.ID)
'						lblFriendMessage.Text = "You cannot add yourself as a friend.";
'					else
'					{
'						user.AcceptCreateFriendship( member.ID );
'						lblFriendMessage.Text = "<b>" + member.Name + "</b> is successfully added to your direct friends list.";
'					}
'				}
'
'				btnCancel.Visible = false;
'				btnAddToFriends.Visible = false;
'				btnAccept.Visible = false;
'				btnReject.Visible = false;
'			}			
'			catch(Exception)
'			{
'				lblFriendMessage.Text = "An unexpected error occured. Please try again.";
'				btnAddToFriends.Visible = false;
'				btnCancel.Visible = false;
'				btnAccept.Visible = false;
'				btnReject.Visible = false;
'			}
'		}
'

		Protected Sub btnReject_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			lblMainTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_AddTitle_TEXT")
			If Request.Params("id") = "" Then
				Response.Redirect(Constants.ERROR_PAGE + "e=" + Constants.[ERROR])
			End If

			Try
				Dim id As Integer = Convert.ToInt32(Request.Params("id"))
				Dim member As User = Users.GetUserByID(id)
				Dim user As User = Me.LoggedUser
				If user.DirectFriends.Contains(member.ID) Then
					lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgUserExist_TEXT")).Replace("[PARAM]", member.Name)

					lblFriendMessage.Visible = True
				Else
					If member.ID = user.ID Then
						lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgCannotAddYou_TEXT")
						lblFriendMessage.CssClass = "main_text_opposite_bold"
					Else
						user.RejectFriendship(member.ID)
						lblFriendMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgNoFriendship_TEXT")).Replace("[PARAM]", member.Name)
					End If
				End If

				btnCancel.Visible = False
				btnAddToFriends.Visible = False
				btnAccept.Visible = False
				btnReject.Visible = False
			Catch generatedExceptionName As Exception
				lblFriendMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgError_TEXT")
				lblFriendMessage.CssClass = "main_text_opposite_bold"
				btnAddToFriends.Visible = False
				btnCancel.Visible = False
				btnAccept.Visible = False
				btnReject.Visible = False
			End Try
		End Sub

		Private Function isAtLeastOneFieldFilled() As Boolean
			Dim sEmail As String = Request.Params(txtEmail.UniqueID).Trim()
			Dim sLastName As String = Request.Params(txtLastName.UniqueID).Trim()

			If sEmail = "" AndAlso sLastName = "" Then
				Return False
			Else
				If (sEmail = member.Email) OrElse (sLastName = member.LastName) Then
					Return True
				Else
					Return False
				End If
			End If
		End Function

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable frmAddFriend.
		====================
        Public ReadOnly Property frmAddFriend() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_frmAddFriend
            End Get
        End Property
        'set { Migrated_frmAddFriend = value; }
    End Class
End Namespace






